import { get, post } from '@/services/base-request';
import { CurrentUser, LoginParams } from '@/types/auth';

/**
 * 登录接口
 * @param body
 */
export const login = async (body: LoginParams) => {
  const { type } = body;
  if (!type || type === 'account') {
    return post('/auth/login', body);
  } else {
    return post('/auth/sms/login', body);
  }
};

/**
 * 退出登录接口
 * @param options
 */
export async function loginOutAPI(options?: { [key: string]: any }) {
  return get('/auth/logout');
}

/**
 * 获取当前的用户
 * @param options
 */
export async function currentUser(options?: { [key: string]: any }) {
  return get<CurrentUser>('/auth/currentUser');
}

/**
 * 获取验证码
 * @param phone
 * @param temCode
 */
export const getFakeCaptcha = async (phone: string, temCode: string) => {
  return get(`/auth/sendSMSCode/${phone}/${temCode}`);
};

/**
 * 验证验证的正确性换取操作Token
 * @param phone
 * @param code
 * @param operation
 */
export const verifyCodeAPI = async (phone: string, code: string, operation: string) => {
  return get(`/auth/verifyCode`, {
    phone,
    code,
    operation,
  });
};

/**
 * 忘记密码
 * @param phone
 * @param operationToken
 * @param newPassword
 */
export const forgetPasswordAPI = async (
  phone: string,
  operationToken: string,
  newPassword: string,
) => {
  return post(`/auth/forgetPassword`, {
    phone,
    operationToken,
    newPassword,
  });
};
